
<template>
  <el-card>
    <!-- 查询条件开始 -->
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="科室名称" prop="deptName">
        <el-select
            v-model="queryParams.deptName"
            placeholder="请选择科室名称"
            clearable size="small"
            style="width:220px">
          <el-option
              v-for="dict in deptOptions"
              :key="dict.deptName"
              :label="dict.deptName"
              :value="dict.deptName"/>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
            v-model="queryParams.status"
            placeholder="可用状态"
            clearable size="small"
            style="width:240px"
        >
          <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
            v-model="queryParams.dateRange"
            size="small"
            style="width:240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholde="开始日期"
            end-placeholde="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini"  @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" size="mini"  @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 查询条件结束 -->

    <!-- 表格工具按钮开始 -->
    <el-row :gutter="10" style="margin-bottom: 8px;">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="batchDelete">
          批量删除
        </el-button>
      </el-col>
    </el-row>
    <!-- 表格工具按钮结束 -->

    <!-- 数据表格开始 -->
    <el-table  border :data="deptTableList" @selection-change="selectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="科室ID" align="center" prop="deptId"/>
      <el-table-column label="科室名称" align="center" prop="deptName"/>
      <el-table-column label="科室编码" align="center" prop="deptNumber"/>
      <el-table-column label="当前挂号量" align="center" prop="regNumber" width="70px"/>
      <el-table-column label="排序码" align="center" prop="orderNum" width="70px" />
      <el-table-column label="负责人" align="center" prop="deptLeader"/>
      <el-table-column label="电话" align="center" prop="leaderPhone"/>
      <el-table-column label="状态" prop="status" align="center" :formatter="statusFormatter" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="200"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button type="text" icon="el-icon-delete" size="mini" :disabled="single" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 数据表格结束 -->
    <!-- 分页控件开始 -->
    <el-pagination
        :current="queryParams.pageNum"
        :page-sizes="[5, 10, 20, 30]"
        :pageSize="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryParams.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    <!-- 分页控件结束 -->

    <!-- 添加修改弹出层开始 -->
    <el-dialog
        :title="title"
        :visible.sync="open"
        width="500px"
        center
        append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="科室名称" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入科室名称" clearable size="small"/>
        </el-form-item>
        <el-form-item label="科室编码" prop="deptNumber">
          <el-input v-model="form.deptNumber" placeholder="请输入科室编码" clearable size="small"/>
        </el-form-item>
        <el-form-item label="挂号开始编号" prop="regNumber">
          <el-input-number v-model="form.regNumber" placeholder="请输入挂号开始编号" clearable size="small"/>
        </el-form-item>
        <el-form-item label="负责人" prop="deptLeader">
          <el-input v-model="form.deptLeader" placeholder="请输入负责人" clearable size="small"/>
        </el-form-item>
        <el-form-item label="电话" prop="leaderPhone">
          <el-input v-model="form.leaderPhone" placeholder="请输入负责人电话" clearable size="small"/>
        </el-form-item>
        <el-form-item label="排序码" prop="orderNum">
          <el-input-number v-model="form.orderNum" clearable size="small"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                :value="dict.dictValue"
            >{{ dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 添加修改弹出层结束 -->

  </el-card>

</template>

<script>
export default {

  name: "Dept",
  data() {
    return {
      // 科室名称字典
      deptOptions:[],
      // 状态字典
      statusOptions: [],
      // 非多个禁用
      multiple: true,
      // 非单个禁用
      single: false,
      // 选中的行
      selectedRows: [],
      // 被删除的数据
      deptIds:[],
      // 表格数据
      deptTableList:[],
      // 表单数据
      form: {},
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      rules: {
        deptName: [
          {required: true, message: '科室名称不能为空', trigger: 'blur'}
        ],
        deptNumber: [
          {required: true, message: '科室编码不能为空', trigger: 'blur'}
        ],
        deptLeader: [
          {required: true, message: '负责人不能为空', trigger: 'blur'}
        ],
        leaderPhone: [
          {required: true, message: '电话不能为空', trigger: 'blur'}
        ],
      },
      // 查询参数
      queryParams: {
        pageNum:1,
        pageSize: 5,
        deptId: undefined,
        deptName: undefined,

      },

    }
  },
  created() {
    //加载所有的部门名字
    this.loadDeptName();
    //加载状态的内容
    this.loadStatus();
    // 查询表格数据
    this.getDeptList();
  },
  methods: {
    selectionChange(val){
      this.selectedRows=val;
      this.multiple = val.length === 0 || val.length === null
      this.single = val.length > 1
    },
    // 分页pageSize变化时触发
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getDeptList()
    },
    // 点击上一页  下一页，跳转到哪一页面时触发
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.loadDept()
    },
    // 打开添加的弹出层
    handleAdd() {
      this.title = '---添加科室信息---'
      this.open = true
      this.reset()
    },
    // 打开修改的弹出层并加载数据
    handleUpdate(row) {
      this.title = '---修改科室信息---'
      const deptId = row.deptId || this.ids
      this.open = true
      this.$http.get("/system/dept/getByDeptId/"+deptId).then(res => {
        if (res.data.code === 200) {
          // this.$message.success("查询成功")
          this.form = res.data.data
        }
      })
    },
    // 增加修改确定
    handleSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          // 做添加
          if (this.form.deptId === undefined) {
            this.$http.post("/system/dept/add_update",this.form).then(res => {
              this.$message.success('---添加成功---')
              this.getDeptList()// 列表重新查询
              this.open = false// 关闭弹出层
            })
          } else { // 做修改
            this.$http.post("/system/dept/add_update",this.form).then(res => {
              this.$message.success('---修改成功---')
              this.getDeptList()// 列表重新查询
              this.open = false// 关闭弹出层
            })
          }
        }
      })
    },
    handleDelete(row) {
      const deptId = row.deptId || this.ids
      this.$confirm('是否确认删除科室编号为"' + deptId + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.get("/system/dept/delete/" + deptId).then(resp => {
          this.$message.success("删除成功");
          this.getDeptList();
        }).catch(error => {
          this.$message.error("删除失败");
          console.error('删除请求失败:', error);
        });
      }).catch(() => {
        this.$message.warning("取消删除");
      });
    },

    //批量删除事件
    batchDelete(){
      this.deptIds=this.selectedRows.map(item=>item.deptId)
        this.$confirm("确认删除选中的"+this.deptIds.length+"项吗？","提示",{
          confirmButtonText:"确定",
          cancelButtonText:"取消",
          type:"warning"
        }).then(()=>{
          this.$http.get("/system/dept/batchDelete?deptIds="+this.deptIds).then(resp=>{
            this.$message.success("删除成功");
            this.getDeptList();
          }).catch(error => {
            this.$message.error("删除失败");
            console.error('删除请求失败:', error);
          });
        }).catch(() => {
          this.$message.warning("取消删除");
        });
      },
    // 条件查询
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getDeptList()
    },
    // 重置查询条件
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 5,
        deptId: undefined,
        deptName: undefined,
        dateRange: []
      }
      this.getDeptList();
    },
    // 格式化状态
    statusFormatter(row){
      let statusName="未知";
      this.statusOptions.filter(item=>{
        if(row.status===item.dictValue){
          statusName=item.dictLabel;
        }
      })
      return statusName;
    },

    // 查询表格数据
    getDeptList() {
      this.$http.post("/system/dept/listByPage",this.queryParams).then(resp=>{
        console.log(resp.data.data)
        this.deptTableList= resp.data.data.records;
        this.queryParams.total=resp.data.data.total;

      })
    },
    // 加载表格内容
    loadDept() {
      this.$http.post("/system/dept/listByPage",this.queryParams).then(resp=>{
        this.deptTableList= resp.data.data.records;
        this.queryParams.total=resp.data.data.total;
      })
    },
    //加载所有的部门
    loadDeptName() {
      this.$http.get("/system/dept/list").then(resp=>{
        // console.log(resp.data.data)
        this.deptOptions=resp.data.data;
      })
    },
    //加载状态的内容
    loadStatus() {
      this.$http.get("/system/dict/data/type/sys_normal_disable").then(resp=>{
        this.statusOptions=resp.data.data;
      })
    },
    // 取消
    cancel() {
      this.resetForm()
      this.open = false
      this.title = ''
    },
    // 重置表单
    reset() {
      this.form = {
        deptId: undefined,
        deptName: undefined,
        deptLeader: undefined,
        deptNumber: undefined,
        orderNum: 0,
        regNumber: 0,
        status: '0'
      }
    },
    resetForm() {
      this.$refs.form.resetFields()
    },


  }
}

</script>
<style scoped>

</style>